package code.Array;

import java.util.Arrays;

/**
 * Given a binary array, find the maximum number of consecutive 1s in this array.
 * <p>
 * Example 1:
 * Input: [1,1,0,1,1,1]
 * Output: 3
 * Explanation: The first two digits or the last three digits are consecutive 1s.
 * The maximum number of consecutive 1s is 3.
 * Note:
 * <p>
 * The input array will only contain 0 and 1.
 * The length of input array is a positive integer and will not exceed 10,000
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/max-consecutive-ones
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class MaxConsecutiveOnes {
    public static void main(String[] args) {
        int[] array = {1, 1, 0, 1, 1, 1};
        int result = findMaxConsecutiveOnes(array);
        System.out.println("Output:" + result);
    }

    public static int findMaxConsecutiveOnes(int[] nums) {
        System.out.println("Input:" + Arrays.toString(nums));
        int dp[] = new int[nums.length];
        if (nums[0] == 1) {
            dp[0] = 1;
        } else {
            dp[0] = 0;
        }
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == 1) {
                dp[i] = dp[i - 1] + 1;
            } else {
                dp[i] = 0;
            }
        }
        int max = 0;
        for (int j = 0; j < dp.length; j++) {
            if (max < dp[j]) {
                max = dp[j];
            }
        }
        return max;
    }
}
